package com.lecture.java8.datastructure;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2021/3/24 10:13
 */
public class Demo3 {

    public static void main(String[] args) {
        f1(100, 1.1);

        double[] a = new double[101];   //定义多项式系数
        a[0] = 1;
        for (int i = 1; i <= 100; i++) {
            a[i] = 1.0 / i;
        }
        funcA(a.length-1, a, 1.1);
        funcB(a.length-1, a, 1.1);
    }

    public static void f1(int n, double x) {
        double p = 1;
        for (int i = 1; i <= n ; i++) {
            p += Math.pow(x, i)/i;
        }
        System.out.println(p);
    }
    //1722.444814169291

    //计算方式一
    public static double funcA(int n, double[] a, double x){
        double p = a[0];
        for (int i = 1; i <= n; i++){
            p += (a[i] * Math.pow(x, i));
        }
        System.out.println(p);
        return p;
    }

    public static double funcB(int n, double[] a, double x){
        double p = a[n];
        for (int i = n; i > 0; i--){
            p = a[i-1] + x * p;
        }
        System.out.println(p);
        return p;
    }
}
